﻿using DBUtil;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
    public class DML
    {
        /// <summary>
        /// 得到实体
        /// </summary>
        public static T GetEntity<T>(string sql, SqlParameter[] parameters = null, DbHelperSQLP DbHelper = null) where T : class,new()
        {
            T entity = null;
            SqlDataReader dr = null;
            if (parameters == null)
            {
                if (DbHelper == null)
                    dr = DbHelperSQL.ExecuteReader(sql);
                else
                    dr = DbHelper.ExecuteReader(sql);
            }
            else
            {
                if (DbHelper == null)
                    dr = DbHelperSQL.ExecuteReader(sql, parameters);
                else
                    dr = DbHelper.ExecuteReader(sql, parameters);
            }
            using (dr)
            {
                if (dr.Read())
                {
                    entity = new T();
                    //对象构造
                    entity = Function.Method.PopulateEntity(entity, dr);
                }
            }
            return entity;
        }

        /// <summary>
        /// 得到实体
        /// </summary>
        public static T GetEntity<T>(SqlDataReader dr) where T : class,new()
        {
            T entity = null;
            using (dr)
            {
                if (dr.Read())
                {
                    entity = new T();
                    //对象构造
                    entity = Function.Method.PopulateEntity(entity, dr);
                }
            }
            return entity;
        }
    }
}
